package qq;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class CopyOfPostToPoco {
	/*一个登录poco图片上传网站控制面板上传面板的备份*/
	String html = null;
	String Cookie = null;
	String host[] = { "www.poco.cn", "my.poco.cn" };
	String url[] = {
			"http://www.poco.cn/module_common/login/poco_login_act.php",
			"http://my.poco.cn/manage/" };

	public CopyOfPostToPoco() {
		HttpClient broser = new HttpClient();

		broser.getHttpConnectionManager().getParams()
				.setConnectionTimeout(9000);

		broser.getHostConfiguration().setProxy("10.37.240.29", 8080); // 代理

		for (int j = 0; j <= 1; ++j) {
			PostMethod post = new PostMethod(url[j]);

			if (Cookie != null)
				post.setRequestHeader("Cookie", Cookie);
			post.setRequestHeader("Host", host[j]);
			post.setRequestHeader(
					"User-Agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; aff-kingsoft-ciba)");
			post.setRequestHeader(
					"Accept",
					"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
			post.setRequestHeader("Accept-Language", "zh-cn");
			post.setRequestHeader("Proxy-Connection", "keep-alive");
			if (j == 0) {
				post.setRequestHeader("Pragma", "no-cache");
				post.setRequestHeader("Referer", "http://www.poco.cn/");
				post.setRequestHeader("Content-type",
						"application/x-www-form-urlencoded");

				NameValuePair act = new NameValuePair("act", "login");
				NameValuePair CookieDate = new NameValuePair("CookieDate", "1");
				NameValuePair dont_remember_login_state = new NameValuePair(
						"dont_remember_login_state", "1");
				NameValuePair locate = new NameValuePair("locate",
						"http://my.poco.cn/manage/");
				NameValuePair login_type = new NameValuePair("login_type", "0");
				NameValuePair pass_word = new NameValuePair("pass_word",
						"199467miao");
				NameValuePair sbmit = new NameValuePair("sbmit", " ¼");
				NameValuePair user_name = new NameValuePair("user_name",
						"125671911@qq.com");
				NameValuePair warm_tag = new NameValuePair("warm_tag", "1");

				NameValuePair[] nvp1 = new NameValuePair[] { act, CookieDate,
						dont_remember_login_state, locate, login_type,
						pass_word, sbmit, user_name, warm_tag };
				post.setRequestBody(nvp1);
			}

			byte[] buff = new byte[1024];
			int state = 0;
			try {
				state = broser.executeMethod(post);

				System.out.println("post.getPath()=" + post.getPath());
				System.out.println("post.getURI()=" + post.getURI());

				for (int i = 0; i < post.getResponseHeaders().length; ++i) {
					System.out.println("post.getResponseHeader(" + i + ")="
							+ post.getResponseHeaders()[i]);
				}

				if (Cookie == null) {
					Cookie += "__POCO_POPER__=1;poper_29021=1;";
					for (int i = 10; i < post.getResponseHeaders().length; ++i) {

						Cookie += post.getResponseHeaders()[i].getValue()
								.replaceAll("(?:expires=|path=)(.|\n)+", "");
					}
				}

				buff = StreamTool.readInputStream(post
						.getResponseBodyAsStream());
				
			} catch (HttpException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

			System.out.println("state=" + state);

			if (state == HttpStatus.SC_OK) {
				try {
					html = new String(buff, "gb2312");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				System.out.println("输出网页内容" + html);
			} else {
				System.out.println("连接失败" + "state=" + state);

				Header[] header = null;

				for (int i = 0; i < (header = post.getResponseHeaders()).length; ++i) {
					System.out.print(header[i].getName() + ":");
					System.out.println(header[i].getValue());
				}
				System.out.println(post.getResponseHeader("Location"));
			}

			post.releaseConnection();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new CopyOfPostToPoco();

	}

}
